package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.CustomerTraceHistory;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.CustomerTraceHistoryQueryObject;
import cn.wolfcode.crm.service.ICustomerTraceHistoryService;
import cn.wolfcode.crm.service.ISystemdictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.UserContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("customerTraceHistory")
@Controller
public class CustomerTraceHistoryController {
    @Autowired
    private ICustomerTraceHistoryService customerTraceHistoryService;
    @Autowired
    private ISystemdictionaryService systemdictionaryService;

    @RequestMapping("list")
    public String list(Model model,@ModelAttribute("qo") CustomerTraceHistoryQueryObject qo) {
        Subject subject = SecurityUtils.getSubject();
        if (!(subject.hasRole("admin") || subject.hasRole("客户经理"))) {
            //拿到当前登录的用户
            Employee emp = UserContext.getCurrentUser();
            //如果当前不是admin或者客户经理,则在qo中设置营销人员(当前用户)的id,增加查询条件
            qo.setSellerId(emp.getId());
        }
        model.addAttribute("pageInfo", customerTraceHistoryService.queryForList(qo));
        model.addAttribute("cms", systemdictionaryService.queryByParentSn("communicationMethod"));
        return "customerTraceHistory/list";
    }

    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(CustomerTraceHistory entity) {
        customerTraceHistoryService.saveOrUpdate(entity);
        return "redirect:/customerTraceHistory/list.do";
    }
}
